<template>
	<view class="navbar" @click="open">
		<view class="navbar-fixed">

			<!-- 导航栏内容 -->
			<view class="navbar-content" :class="{search:isSearch}"
				:style="{height:navBarHeight+'px',width:navBarWidth+'px'}">
				<view class="hello" v-if="!isSearch">
					你好，码农!
				</view>
				<!-- 仅在搜索页 -->
				<view class="navbar-content-search-back" v-else="isSearch" @click="back">
					<uni-icons type="back" size="26" color="#155915"></uni-icons>
				</view>
				<view class="searchblock" v-if="isSearch">
					<input type="text" v-model="val" @input="inputChange" placeholder="请输入您要了解的技术...">
				</view>

				<view class="tab-icons" v-if="!isSearch">
					<uni-icons type="search" size="24" color="#155915"></uni-icons>
				</view>
			</view>
		</view>
		<view :style="{height:statusBarHeight+navBarHeight+'px'}"></view>
	</view>
</template>

<script>
	export default {
		props: {
			// 判断是否再搜索状态
			isSearch: {
				type: Boolean,
				default: false
			},
			//声明接收
			value:{
				type:[String,Number] ,//两种类型
				default:''
			}
		},
		//监听数据的改变
		watch:{
			value(newVal){
				this.val = newVal
			}
		},
		name: "navbarIndex",
		data() {
			return {
				statusBarHeight: 20,
				navBarHeight: 45,
				windowWidth: 375,
				val:''

			};
		},
		methods: {
			open() {
				if (this.isSearch) return
				uni.navigateTo({
					url: "/pages/searchHome/searchHome"
				})
				console.log("sousuo");
			},
			inputChange(e){
				const {value} = e.detail
				// console.log(value);
				//将数据发送出去 用input把value数据带出去
				this.$emit('input',value)
			},
			back(){
				uni.switchTab({
					url:'/pages/tabbar/index/index'
				})
			}
		},

		created() {
			//同步获取手机系统信息
			const info = uni.getSystemInfoSync()
			//设置状态栏高度(H5的状态栏高度是0)
			this.statusBarHeight = info.statusBarHeight
			this.navBarWidth = info.windowWidth
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			//H5 app mp-alipay 不支持uni.getMenuButtonBoundingClientRect()这个api
			//获取胶囊的位置信息
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			//(胶囊底部高度-状态栏高度)+（胶囊顶部高度-状态栏高度）=导航栏高度
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - inf
				.statusBarHeight) + 4
			//内容宽度
			this.navBarWidth = menuButtonInfo.left
			// #endif

		}

	}
</script>

<style lang="scss">
	@import "../../uni_modules/uni-icons/components/uni-icons/uniicons.css";

	.navbar {
		// margin-top: 20px;
		// z-index:100;
		.statebar {
			height: 20px;
		}

		.navbar-fixed {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 0 15px;
			width: 100%;
			height: 70px;
			
			background-color: white;
			// background-image: linear-gradient();
			// 在盒内显示
			box-sizing: border-box;

			.navbar-content {
				display: flex;
				position: relative;
				justify-content: center; //水平居中
				align-items: center;
				padding: 0 15px;
				margin-top:-30px;
				// margin-bottom: 10px;
				width: 500px;
				box-sizing: border-box; //只在盒内显示

				.hello {
					position: absolute;
					// display: block;
					left: 0;
					top: 0;
					// padding: 30px;
					margin-top: 20px;
					// width: 100%;
					font-weight: 1000;
					font-size: 24px;
					color: $mk-base-color;
					box-sizing: border-box;
				}

				.tab-icons {
					display: flex;
					position: absolute;
					top: -40%;
					right: 0%;
					justify-content: center;
					align-items: center;
					width: 35px;
					padding: 2px;
					// height: 35px;
					margin-top: 40px;
					// background-color: #f8fcfc;
					border-radius: 6px;

					// border: 1px #f3f3f3 solid;
					// border: 1px green solid;

				}

				&.search {
					padding-left: 0;
					position: relative;

					.navbar-content-search-back {
						margin-left: 0px;
						margin-right: 10px;
						position: absolute;
						top: 54%;
						left: 0;
						// border: 2px solid red;
					}

					.searchblock {
						border: 1px solid #e1e1e1;
						display: flex;
						position: absolute;
						top: 42%;
						left: 10%;
						align-items: center;
						padding: 0 10px;
						width: 80%;
						height: 38px;
						border-radius: 20px;
						padding-left: 15px;
						background-color: #fff;

						.navbar-search_text {
							width: 100%;
							font-size: 12px;
							color: #e1e1e1;
						}
					}
				}
			}


		}

	}
</style>
